<?php
//后台登陆控制
include_once('admin_global.php');
$db->Get_user_shell_check($uid, $shell);   //检测用户是否登录
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>编辑文章</title>
    <link rel="stylesheet" href="css/right.css" type="text/css" />
    <script type="text/javascript" src="../kindeditor/kindeditor.js"></script>
    <script type="text/javascript" src="../kindeditor/lang/zh_CN.js"></script>
    <script>
        KindEditor.ready(function(K) {
            var editor = K.create('textarea[name="content"]', {
                cssPath : '../kindeditor/plugins/code/prettify.css',
                uploadJson : '../kindeditor/php/upload_json.php',
                fileManagerJson : '../kindeditor/php/file_manager_json.php',
                allowFileManager : true,
                afterBlur: function(){this.sync();},              //编辑器失去焦点后同步KindEditor的值到textarea文本框，这一行很重要。
                afterCreate : function() {
                    var self = this;
                    K.ctrl(document, 13, function() {
                        self.sync();
                        K('form[name=example]')[0].submit();
                    });
                    K.ctrl(self.edit.doc, 13, function() {
                        self.sync();
                        K('form[name=example]')[0].submit();
                    });
                }
            });
            prettyPrint();
        });
    </script>
    <script>
        window.onload=function(){
            var myform=document.getElementById("form1");
            myform.onsubmit=check_editNews;
            function check_editNews(){
                if(myform.newsTitle.value==""){
                    alert("请输入文章标题！");
                    myform.newsTitle.focus();
                    return false;
                }
                if(myform.pid.value==0){
                    alert("请选择文章所属栏目");
                    return false;
                }
                if(myform.newsAuthor.value==""){
                    alert("请输入文章作者！");
                    myform.newsAuthor.focus();
                    return false;
                }
                if(myform.content.value==""){
                    alert('请输入文章内容！');
                    return false;
                }
            }
        }
    </script>
</head>
<?php
//显示树形栏目分类
$sql = "SELECT * FROM category";
$query = $db->query($sql);
while ($row = $db->fetch_array($query)) {
    $data[] = $row;
}
$newsID = $_GET['newsID'];       //获得文章ID
$sql = "SELECT newsCID FROM news WHERE newsID='$newsID'";
$query = $db->query($sql);
while ($row = $db->fetch_array($query)) {
    $cateID = $row['newsCID'];   //获得文章栏目ID
}
$tree = new Tree($data);
$gettree = $tree->get_tree(0, "<option value=\$id \$selected>\$spacer\$cateName</option>", $cateID);

//如果是单页栏目的文章
$sql="SELECT * FROM category WHERE id=$cateID";
$query=$db->query($sql);
$row=$db->fetch_array($query);
if($row['cateStyleID']==2){
    echo "<script>alert('不能在此编辑单页栏目文章！');history.go(-1);</script>";
    exit();
}
//读取文章内容
$sql="SELECT * FROM news WHERE newsID='$newsID'";
$query=$db->query($sql);
while ($row = $db->fetch_array($query)) {
    $rs = $row;
}

//修改文章内容
if (isset($_POST['editNews'])) {
    $cateID = $_POST['pid'];
    $sql1 = "SELECT * FROM category WHERE catePID=$cateID";
    $query1 = $db->query($sql1);
    $num = $db->db_num_rows($query1);
    $sql2 = "SELECT * FROM category WHERE id=$cateID";
    $query2 = $db->query($sql2);
    $row = $db->fetch_array($query2);
    if ($num > 0) {
        echo "<script>alert('不能选择含有子栏目的栏目！');</script>";
    } elseif ($row['cateStyleID'] == 2) {
        echo "<script>alert('不能选择单页栏目！');</script>";
    } else {
        //获取POST数组变量
        $newsTitle = trim($_POST['newsTitle']);
        $newsKeyWords = $_POST['newsKeyWords'];
        $newsAuthor = $_POST['newsAuthor'];
        $newsSource = $_POST['newsSource'];
        if (!empty($_POST['isTop'])) {
            $isTop = 1;
        } else {
            $isTop = 0;
        }
        if (!empty($_POST['isRed'])) {
            $isRed = 1;
        } else {
            $isRed = 0;
        }
        $newsUpdateTime = time();
        //判断是否有图片
        $newsContentTMP = $_POST['content'];
        //$newsContentJJ=  mb_substr(strip_tags($newsContentTMP),0,90,'utf-8');
        $patten = '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i';
        if (preg_match($patten, $newsContentTMP, $matche)) {
            $newsImagePath = $matche[1];
        } else {
            $newsImagePath = "";
        }
        if (!get_magic_quotes_gpc()) {
            $newsContent = addslashes($_POST['content']);
        } else {
            $newsContent = $_POST['content'];
        }
        //写入数据库
        $sql = "UPDATE news SET newsCID=$cateID,newsAuthor='$newsAuthor',newsTitle='$newsTitle',newsSource='$newsSource',isTop=$isTop,isRed=$isRed,newsUpdateTime=$newsUpdateTime,newsImagePath='$newsImagePath',newsKeyWords='$newsKeyWords',newsContent='$newsContent' WHERE newsID=$newsID";
        $query = $db->query($sql);
        if ($query == true) {
            if($_SESSION['userType']=='普通管理员'){
                echo "<script>alert('文章修改成功！');window.location.href='listMyNews.php'</script>";
            }else{
                echo "<script>alert('文章修改成功！');window.location.href='listNews.php'</script>";
            }
        } else {
            echo "<script>alert('文章修改失败！');history.go(-1);</script>";
        }
    }
}
?>
<body>
<div id="right_main">
    <div class="right_content">
        <div class="right_top_menu">
            <h3>
                您当前的位置：后台 > 文章管理 > 编辑文章
            </h3>
        </div>
        <div class="right_center_content">
            <form id="form1" method="post" action="">
                <table cellpadding="0" cellspacing="0" border="0" width="100%">
                    <tr>
                        <td valign="top" >
                            <div class="txl_search"></div>
                            <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                <tr>
                                    <th colspan="2" >编辑文章</th>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">所属栏目：</td>
                                    <td width="90%">
                                        <select name="pid">
                                            <option value="0">请选择文章所属栏目</option>
                                            <?php print_r($gettree); ?>
                                        </select> *不能指定为含有子栏目的栏目！
                                    </td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章标题：</td>
                                    <td width="90%"><input type="text" name="newsTitle"  class="int_text" value="<?php echo $rs['newsTitle']; ?>" size="60"  maxlength="60"/> *文章标题不能少于6个字符！</td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">关键字：</td>
                                    <td width="90%"><input type="text" name="newsKeyWords"  class="int_text" value="<?php echo $rs['newsKeyWords']; ?>" size="40"  maxlength="40"/></td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章作者：</td>
                                    <td width="90%"><input type="text" name="newsAuthor"  class="int_text" value="<?php echo $rs['newsAuthor']; ?>" size="40"  maxlength="40"/> *必填</td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章来源：</td>
                                    <td width="90%"><input type="text" name="newsSource"  class="int_text" value="<?php echo $rs['newsSource']; ?>" size="40"  maxlength="40"/></td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">是否置顶：</td>
                                    <td width="90%"><input type="checkbox" name="isTop"  class="int_text" value="6" size="40" <?php if ($rs['isTop'] == 1) {  echo "checked='checked'";  } ?>/></td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">是否加红：</td>
                                    <td width="90%"><input type="checkbox" name="isRed"  class="int_text" value="6" size="40" <?php if ($rs['isRed'] == 1) {  echo "checked='checked'";  } ?>/></td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章内容：</td>
                                    <td width="90%"><textarea id="editor1" class=".int_area" name="content" style="width:95%;height:400px;"><?php echo $rs['newsContent']; ?></textarea></td>
                                </tr>
                                <tr>
                                    <td colspan="2" align="center" height='30'>
                                        <input type="submit" name="editNews" class="btn_ipt01" value=" 确认修改 "/>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
</div>
</body>
</html>
